﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;

namespace OChatWCFLib
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in both code and config file together.
    public class OChatService : IOChatService
    {
        List<OChatSession> chat_session = new List<OChatSession>();

        public void RegiterSession(string ip, string email, string senha)
        {
            OChatSession chat = new OChatSession();
            chat.Ip = ip;
            chat.Email = email;
            chat.Senha = senha;

            chat_session.Add(chat);            
        }

        public void SendMsg(string value)
        {
            foreach(OChatSession chat in chat_session)
            {
                Task<string> response = SendRequest(chat.Ip, 8733, "Sender", "Teste de sender");
            }
        }

        class OChatSession
        {
            string ip = string.Empty;
            public string Ip
            {
                get { return ip; }
                set { ip = value; }
            }

            string email = string.Empty;
            public string Email
            {
                get { return email; }
                set { email = value; }
            }

            string senha = string.Empty;
            public string Senha
            {
                get { return senha; }
                set { senha = value; }
            }
        }

        private static async Task<string> SendRequest(string server, int port, string method, string data)
        {
            try
            {
                IPAddress ipAddress = null;
                IPHostEntry ipHostInfo = Dns.GetHostEntry(server);
                for (int i = 0; i < ipHostInfo.AddressList.Length; ++i)
                {
                    if (ipHostInfo.AddressList[i].AddressFamily ==
                      AddressFamily.InterNetwork)
                    {
                        ipAddress = ipHostInfo.AddressList[i];
                        break;
                    }
                }
                if (ipAddress == null)
                    throw new Exception("No IPv4 address for server");
                TcpClient client = new TcpClient();
                await client.ConnectAsync(ipAddress, port); // Connect
                NetworkStream networkStream = client.GetStream();
                StreamWriter writer = new StreamWriter(networkStream);
                StreamReader reader = new StreamReader(networkStream);
                writer.AutoFlush = true;
                string requestData = "method=" + method + "&" + "data=" +
                  data + "&eor"; // 'End-of-request'
                await writer.WriteLineAsync(requestData);
                string response = await reader.ReadLineAsync();
                client.Close();
                return response;
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

    }


}
